package com.example.yskjproject.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.yskjproject.R;
import com.example.yskjproject.adapter.OrderPageAdapter;
import com.example.yskjproject.adapter.WithDrawalRecordAdapter;
import com.example.yskjproject.content.Content;
import com.example.yskjproject.http.Urls;
import com.example.yskjproject.http.callback.JsonCallback;
import com.example.yskjproject.model.LzyResponse;
import com.example.yskjproject.model.OrderListModel;
import com.example.yskjproject.model.WithDrawalRecordListModel;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.Arrays;
import java.util.List;

import io.reactivex.annotations.Nullable;

public class OrderInfoPageFragment extends Fragment {
    private static final String ARG_POSITION = "position";
    private int position;
    private View mOrderPageFragment;
    private RecyclerView mOrderPageRecyclerView;
    private OrderPageAdapter mOrderPageAdapter;
    private SwipeRefreshLayout swipeRefreshLayout;
    public static OrderInfoPageFragment newInstance(int position) {
        OrderInfoPageFragment fragment = new OrderInfoPageFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_POSITION, position);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        position = getArguments() != null ? getArguments().getInt(ARG_POSITION) : 0;

        mOrderPageFragment = inflater.inflate(R.layout.order_page_fragment, container, false);
        mOrderPageRecyclerView = mOrderPageFragment.findViewById(R.id.rl_order_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        mOrderPageRecyclerView.setLayoutManager(layoutManager);
        mOrderPageAdapter = new OrderPageAdapter();
        swipeRefreshLayout = mOrderPageFragment.findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setRefreshing(true);
        switch (position) {
            case 0: // 全部
                getAllOrderList(Arrays.asList("1","2","3"),1);
                swipeRefreshLayout.setOnRefreshListener(() -> getAllOrderList(Arrays.asList("1","2","3"),1));
                break;
            case 1: // 消费订单
                getAllOrderList(Arrays.asList("1"),2);
                swipeRefreshLayout.setOnRefreshListener(() -> getAllOrderList(Arrays.asList("1"),1));
                break;
            case 2: // 收入订单
                getAllOrderList(Arrays.asList("2"),3);
                swipeRefreshLayout.setOnRefreshListener(() -> getAllOrderList(Arrays.asList("2"),1));
                break;
            default:
        }

        return mOrderPageFragment;
    }


    // 全部订单
    private void getAllOrderList(List<String> searchType,int type) {
        OkGo.<LzyResponse<OrderListModel>>get(Urls.ORDER_LIST ).addUrlParams("search_type",searchType).params("page", Content.page).params("limit", Content.limit).execute(new JsonCallback<LzyResponse<OrderListModel>>() {

            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onSuccess(Response<LzyResponse<OrderListModel>> response) {
                if (mOrderPageRecyclerView.getChildCount() > 0) {
                    mOrderPageRecyclerView.removeAllViews();
                    mOrderPageAdapter.notifyDataSetChanged();
                }
                OrderListModel orderListModel = response.body().data;
                if (!orderListModel.getOrder_list().isEmpty()) {
                    mOrderPageAdapter.addData(orderListModel.getOrder_list(),getContext());
                    mOrderPageRecyclerView.setAdapter(mOrderPageAdapter);
                }
                swipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onError(Response<LzyResponse<OrderListModel>> response) {
                super.onError(response);
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }
}
